package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.Material;
import com.sevattal.service.leaves.mapper.MaterialMapper;
import com.sevattal.service.leaves.service.MaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 物资表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Override
    public boolean createMaterial(Material material) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_mId", material.getFMid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.insert(material);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateMaterial(Material material) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_mId",material.getFMid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count == 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        if (count == 1){
            int result = baseMapper.update(material,queryWrapper);
            return  result == 1;
        }
        return false;
    }

    @Override
    public boolean deleteMaterial(String id) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_mId",id);
        int result = baseMapper.delete(queryWrapper);
        return result == 1;
    }

    @Override
    public List<Material> getMaterialList(Material material) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("f_mId");

        if (material.getFMid() != null && !StringUtils.isEmpty(material.getFMid())){
            queryWrapper.eq("f_mId",material.getFMid());
        }
        if (material.getFMname() != null && !StringUtils.isEmpty(material.getFMname())){
            queryWrapper.eq("f_mName",material.getFMname());
        }
        if (material.getFMkind() != null){
            queryWrapper.eq("f_mKind",material.getFMkind());
        }
        if (material.getFMbrand() != null && !StringUtils.isEmpty(material.getFMbrand())){
            queryWrapper.eq("f_mBrand",material.getFMbrand());
        }
        if (material.getFMtype() != null && !StringUtils.isEmpty(material.getFMtype())){
            queryWrapper.eq("f_mType",material.getFMtype());
        }
        List<Material> materialList = baseMapper.selectList(queryWrapper);
        return materialList;
    }

    @Override
    public Material getMaterial(String id) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_mId",id);
        Material material = baseMapper.selectOne(queryWrapper);
        return material;
    }
}
